package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentScoreDAO;
import cn.edu.nju.software.greencreditsystem.business.CoinEnvironmentAnalyser;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentScore;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-18
 * 类描述：EnvironmentScore类DAO实现
 */
public class EnvironmentScoreDAOImpl implements EnvironmentScoreDAO
{
    @Override
    public boolean save(EnvironmentScore es)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(es);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(EnvironmentScore es)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(es);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(EnvironmentScore es)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(es);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnvironmentScore getById(int id)
    {
        EnvironmentScore result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (EnvironmentScore) s.get(EnvironmentScore.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentScore> getLatest(int enterpriseID, int environmentFactorID, int num)
    {
        List<EnvironmentScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnvironmentScore "
                                    + "where enterprise.id=" + enterpriseID
                                    + " and factor.id=" + environmentFactorID
                                    + " order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        if(num ==0)
            return result;
        else
            return result.subList(0, Math.min(result.size(), num));
    }

    @Override
    public double getMax(int enterpriseID, int environmentFactorID)
    {
        double result = -1;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("select max(value) from EnvironmentScore "
                                    + "where enterprise.id="+enterpriseID
                                    + " and factor.id=" + environmentFactorID);
            
            result = (Double) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double getMin(int enterpriseID, int environmentFactorID)
    {
        double result = -1;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("select min(value) from EnvironmentScore "
                                    + "where enterprise.id="+enterpriseID
                                    + " and factor.id=" + environmentFactorID);
            
            result = (Double) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentScore> getHistoryScore(int enterpriseID, int environmentFactorID, Date begin, Date end)
    {
        List<EnvironmentScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnvironmentScore "
                                    + "where enterprise.id = " + enterpriseID
                                    + " and factor.id = " + environmentFactorID
                                    + " and date >= :begin and date <= :end");
            q.setDate("begin", begin);
            q.setDate("end", end);
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double getIncreaseNumber(int enterpriseID, int environmentFactorID)
    {
        List<EnvironmentScore> list = getLatest(enterpriseID, environmentFactorID, 2);
        if(list.size() > 1)
            return list.get(0).getValue() - list.get(1).getValue();
        else
            return 0;
    }

    @Override
    public double getIncreaseMargin(int enterpriseID, int environmentFactorID)
    {
        List<EnvironmentScore> list = getLatest(enterpriseID, environmentFactorID, 2);
        if(list.size() > 1)
            return (list.get(0).getValue() - list.get(1).getValue()) / list.get(1).getValue();
        else
            return 0;
    }

    @Override
    public double getLatestTotalScore(int enterpriseID)
    {
        double result = 0;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            for(int i = 0; i < 5; i++)
            {
                Query q = s.createQuery("from EnvironmentScore "
                                        + "where enterprise.id = " + enterpriseID
                                        + " and factor.name = '" + CoinEnvironmentAnalyser.indexNames[i] + "'"
                                        + " order by date desc");
                if(q.list().size() > 0)
                    result += ((EnvironmentScore) q.list().get(0)).getValue();
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result/1.66;
    }
}
